package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        //条件：type不能为0
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        //wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
    /**
     * 修改分类（菜品，套餐）
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改的参数:{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }


    /**
     * 删除分类
     * @param id
     * @return
     */
     @DeleteMapping
     public R<String> delete(@RequestParam("ids") Long id){
         log.info("请求删除分类参数：{}",id);
        // categoryService.removeById(id);
         categoryService.remove(id);
         return R.success("删除成功~");
     }

    /**
     * 分类管理的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        log.info("第几页:{},每页显示条数：{}",page,pageSize);
        Page<Category> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);

        categoryService.page(pageInfo);
        return R.success(pageInfo);
    }
    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("请求参数:{}",category.toString());
        categoryService.save(category);
        return R.success("新增菜品成功~");
    }
}
